//
//  JAccomplishOrderCell.m
//  HeadIndustry
//
//  Created by jun on 2017/9/11.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JAccomplishOrderCell.h"
#import "JPayProductView.h"

@interface JAccomplishOrderCell()

@property (nonatomic, assign) NSInteger productNum;
@property (nonatomic, strong) NSMutableArray *cellViewArr;
@property (nonatomic, assign) CGFloat lastMaxY;
@property (nonatomic, strong) UILabel *verdonNameLa;
@property (nonatomic, strong) CALayer *rightImg;
@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, strong) YYLabel *statuela;

@end

@implementation JAccomplishOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.opaque = NO;
        _isFirst = NO;
        self.backgroundColor = [UIColor whiteColor];
        [self configBgCellView];
    }
    return self;
}

- (void)configBgCellView
{
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, self.contentView.width, 45);
    [self.contentView addSubview:topView];
    
    CALayer *layer = [CALayer layer];
    layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"hgome"].CGImage);
    layer.frame = CGRectMake(15, 11, 20, 20);
    [topView.layer addSublayer:layer];
    
    UILabel *namela = [UILabel new];
    namela.frame = CGRectMake(layer.right + 3, 0, 80, topView.height);
    namela.font = kFONT_TITLE(16);
    [topView addSubview:namela];
    namela.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (_block) {
            _block(10, self.waitPayModel);
        }
    }];
    [namela addGestureRecognizer:tap];
    _verdonNameLa = namela;
    
    CALayer *rightImg = [CALayer layer];
    rightImg.contents = (__bridge id _Nullable)([UIImage imageNamed:rightBtnImg].CGImage);
    rightImg.frame = CGRectMake(namela.right + 3, (45 - 16)/2.0, 10, 16);
    [topView.layer addSublayer:rightImg];
    _rightImg = rightImg;
    
    YYLabel *statuLa = [YYLabel new];
    statuLa.frame = CGRectMake(kWindowW - 15 - 100, 0, 100 , 40);
    statuLa.font = kFONT_TITLE(16);
    statuLa.textColor = [UIColor redColor];
    statuLa.textAlignment = NSTextAlignmentRight;
    [topView addSubview:statuLa];
    statuLa.text = [NSString stringWithFormat:@"%@",_waitPayModel.OrderStatus];
    _statuela = statuLa;

}

- (void)configButtomView:(CGFloat)minY
{
    UILabel *productNumLa = [UILabel new];
    productNumLa.frame = CGRectMake(15, minY, 100 , 40);
    productNumLa.font = kFONT_TITLE(14);
    //    productNumLa.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"共%ld件商品",(long)_waitPayModel.Qty];
    [productNumLa sizeToFit];
    productNumLa.frame = CGRectMake(15, minY, productNumLa.width , 40);
    
    // 小计
    YYLabel *SumPriceLaText = [YYLabel new];
    SumPriceLaText.frame = CGRectMake(productNumLa.right +10, minY, 40, 40);
    SumPriceLaText.font = kFONT_TITLE(14);
    [self.contentView addSubview:SumPriceLaText];
    SumPriceLaText.text = @"合计:";
    
    // 小计
    YYLabel *SumPriceLa = [YYLabel new];
    SumPriceLa.frame = CGRectMake(SumPriceLaText.right, minY, 80, 40);
    SumPriceLa.font = kFONT_TITLE(14);
    SumPriceLa.textColor = [UIColor redColor];
    [self.contentView addSubview:SumPriceLa];
    SumPriceLa.text = _waitPayModel.TotalMoneyText;
    
    YYLabel *productFreight = [YYLabel new];
    productFreight.frame = CGRectMake(SumPriceLa.right, minY, 100 , 40);
    productFreight.font = kFONT_TITLE(14);
    //    productFreight.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:productFreight];
    productFreight.text = [NSString stringWithFormat:@"(含运费 ¥0.00)"];
    
    CALayer *line3 = [CALayer layer];
    line3.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line3.frame = CGRectMake(0, productNumLa.bottom - 0.5, kWindowW, 0.5);
    [self.contentView.layer addSublayer:line3];
    
}

- (void)setWaitPayModel:(JWaitPayModel *)waitPayModel
{
    _waitPayModel = waitPayModel;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        _verdonNameLa.text = _waitPayModel.VendorName;
        _statuela.text = [NSString stringWithFormat:@"%@",_waitPayModel.OrderStatus];

        
        [_verdonNameLa sizeToFit];
        CGFloat height = _verdonNameLa.height;
        CGFloat widht = _verdonNameLa.width;
        
        _verdonNameLa.frame = CGRectMake(38, (45 - height)/2.0, widht, _verdonNameLa.height);
        _rightImg.frame = CGRectMake(_verdonNameLa.right + 3, _rightImg.origin.y, _rightImg.width, _rightImg.height);
        
        
        if (_isFirst == NO) {
            
            _isFirst = YES;
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                for (int i = 0; i < waitPayModel.OrderItemModels.count; i++) {
                    JPayProductView *cellView = [[JPayProductView alloc]initWithFrame:CGRectMake(0, 45 + i * (100 + 10), kWindowW, 100)];
//                    [self.contentView addSubview:cellView];
                    //                    [self.cellViewArr addObject:cellView];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.contentView addSubview:cellView];

                        [cellView reloadOrderDataWithModel:waitPayModel.OrderItemModels[i]];
                        
                    });
                    
                }
            });
            
            _lastMaxY = 45 + waitPayModel.OrderItemModels.count * (100 + 10);
            
            
            [self configButtomView:_lastMaxY];
        }
        
        //        _productNum = waitPayModel.OrderItemModels.count;
        
    });
    
}

//
//- (void)payBtnClick:(UIButton *)btn
//{
//    if (_block) {
//        _block(btn.tag);
//    }
//}

- (CGFloat)getHeight
{
    return _lastMaxY + 40;
}

- (NSMutableArray *)cellViewArr
{
    if (_cellViewArr == nil) {
        _cellViewArr = [NSMutableArray array];
    }
    return _cellViewArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
